package com.chengfei.thread;

/**
 * @author chengfei5
 * @date 2019/1/3
 */
public class NewThread implements Runnable{
    @Override
    public synchronized void run() {
        while (true){
            System.out.println("自定义的线程启动了");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        //初始化状态
        NewThread newThread = new NewThread();

        Thread thread = new Thread(newThread);
        //ready-to-run状态(并没有真正开始执行，只是在等待抢占资源)
        thread.start();
        while (true){
            synchronized (newThread){
                System.out.println("主线程执行了");
                try {
                    //当时间到了之后自动进入等待状态
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                newThread.notifyAll();

            }
        }
    }
}
